﻿#include "view.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#pragma execution_character_set("utf-8")

void View::layout(int ** arr, int optimalScore)
{   
    startMusic();

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            p_Number[i*4+j] = new QLabel;
            glayout->addWidget(p_Number[i*4+j], i, j);
        }
    }

    setCurrentScoreLabel();
    setOptimalScoreLabel(optimalScore);
    setTipLabel();
    renewNumber(arr);
    setButton();
    setVoiceLabel();
    setVoiceSlider();

    hlayout1->addWidget(ai);
    hlayout1->addWidget(restart);
    hlayout1->addWidget(tip);
    hlayout1->addWidget(p_Tip);

    hlayout2->addWidget(p_Voice);
    hlayout2->addWidget(voiceSlider);

    vlayout1->addLayout(hlayout1);
    vlayout1->addLayout(hlayout2);

    hlayout3->addLayout(vlayout1);
    hlayout3->addWidget(p_CurrentScore);
    hlayout3->addWidget(p_OptimalScore);
    hlayout3->addSpacerItem(horizontalSpacer);

    mainlayout->addLayout(hlayout3);
    mainlayout->addLayout(glayout);
}

void View::setCurrentScoreLabel()
{
    p_CurrentScore->setText("Score: 0");

    // 设置面板中内容的对齐方式，下同
    p_CurrentScore->setAlignment(Qt::AlignCenter);

    // 设置面板最小宽度、高度，下同
    p_CurrentScore->setMinimumWidth(160);
    p_CurrentScore->setMinimumHeight(40);

    // 设置面板的字体，下同
    QFont textFont;
    textFont.setPointSize(14);            // 字体大小
    textFont.setBold(1);                  // 字体加粗
    textFont.setFamily("MV Boli");        // 字体种类
    p_CurrentScore->setFont(textFont);

    // 设置面板字体的颜色，下同
    p_CurrentScore->setStyleSheet("color:rgb(0,200,200)");
}

void View::setOptimalScoreLabel(int optimalScore)
{
    p_OptimalScore->setText("BestScore: " + QString::number(optimalScore));

    p_OptimalScore->setAlignment(Qt::AlignCenter);

    p_OptimalScore->setMinimumWidth(205);

    QFont textFont;
    textFont.setPointSize(14);
    textFont.setBold(1);
    textFont.setFamily("MV Boli");
    p_OptimalScore->setFont(textFont);

    p_OptimalScore->setStyleSheet("color:rgb(255,100,0)");
}

void View::setTipLabel()
{
    p_Tip->setText("Tip: ");

    p_Tip->setAlignment(Qt::AlignCenter);

    p_Tip->setMinimumWidth(100);

    QFont textFont;
    textFont.setPointSize(16);
    textFont.setBold(1);
    textFont.setFamily("MV Boli");
    p_Tip->setFont(textFont);

    p_Tip->setStyleSheet("color:rgb(180,0,180)");
}

void View::setButton()
{
    QFont textFont;
    textFont.setPointSize(15);
    textFont.setBold(true);

    // 设置按钮的背景色
    ai->setStyleSheet("background:rgb(120,120,120)");
    tip->setStyleSheet("background:rgb(120,120,120)");
    restart->setStyleSheet("background:rgb(120,120,120)");

    ai->setText("🤖");
    ai->setMinimumHeight(50);
    ai->setMaximumWidth(50);
    ai->setFont(textFont);

    tip->setText("💡");
    tip->setMinimumHeight(50);
    tip->setMaximumWidth(50);
    tip->setFont(textFont);

    restart->setText("↻");
    restart->setMinimumHeight(50);
    restart->setMaximumWidth(50);
    restart->setFont(textFont);
}

void View::setGameoverLabel()
{
    p_Gameover->setText("Gameover😭");

    p_Gameover->setAlignment(Qt::AlignCenter);

    p_Gameover->setMaximumWidth(220);

    QFont textFont;
    textFont.setPointSize(18);
    textFont.setBold(1);
    textFont.setFamily("MV Boli");
    p_Gameover->setFont(textFont);

    QPalette pl = p_Gameover->palette();
    pl.setColor(QPalette::WindowText, QColor(Qt::black));
    p_Gameover->setPalette(pl);
}

void View::setVictoryLabel()
{
    p_Victory->setText("🥂Victory🥳");

    p_Victory->setAlignment(Qt::AlignCenter);

    p_Victory->setMaximumWidth(220);

    QFont textFont;
    textFont.setPointSize(18);
    textFont.setBold(1);
    textFont.setFamily("MV Boli");
    p_Victory->setFont(textFont);

    QPalette pl = p_Victory->palette();
    pl.setColor(QPalette::WindowText, QColor(Qt::red));
    p_Victory->setPalette(pl);
}

void View::setComboLabel()
{
    p_Combo->setAlignment(Qt::AlignCenter);

    p_Combo->setMaximumWidth(240);

    QFont textFont;
    textFont.setPointSize(18);
    textFont.setBold(1);
    textFont.setFamily("MV Boli");
    p_Combo->setFont(textFont);

    QPalette pl = p_Combo->palette();
    pl.setColor(QPalette::WindowText, QColor(Qt::blue));
    p_Combo->setPalette(pl);
}

void View::setVoiceLabel()
{
    QFont textFont;
    textFont.setPointSize(20);

    p_Voice->setText("🔈");

    p_Voice->setMaximumWidth(25);

    p_Voice->setAlignment(Qt::AlignCenter);

    p_Voice->setFont(textFont);
}

void View::setVoiceSlider()
{
    voiceSlider = new QSlider(Qt::Horizontal);  // 水平方向
    voiceSlider->setMinimum(0);  // 最小值
    voiceSlider->setMaximum(80);  // 最大值
    voiceSlider->setSingleStep(1);  // 步长
    voiceSlider->setValue(40);
    voiceSlider->setMaximumWidth(200);

    voiceSlider->setStyleSheet("QSlider::handle:horizontal{width:24px;background-color:rgb(255,255,255);margin:-11px 0px -11px 0px;border-radius:12px;}"  // 滑槽属性
                               "QSlider::groove:horizontal{height:2px;background-color:rgb(219,219,219);}"  // 滑块属性
                               "QSlider::add-page:horizontal{background-color:rgb(180,180,180);}"  // 未滑过滑槽属性
                               "QSlider::sub-page:horizontal{background-color:rgb(255,120,0);}");  // 已滑过滑槽属性
}

void View::displayGameover()
{    
    setGameoverLabel();
    hlayout3->addWidget(p_Gameover);  // 若游戏结束，显示Gameover面板
}

void View::displayVictory()
{
    setVictoryLabel();
    hlayout3->addWidget(p_Victory);  // 若游戏通关，显示Victory面板
}

void View::displayCombo(int comboNumber)
{
    setComboLabel();
    p_Combo->setText("🎉Combo×" + QString::number(comboNumber) + "😆");
    hlayout3->addWidget(p_Combo);
}

void View::startMusic()
{
    clog<<"start";
    // 将背景音乐循坏播放
    musiclist = new QMediaPlaylist;
    musiclist->addMedia(QUrl::fromLocalFile("../../resource/2048bj/bj_music3.wav"));  // 背景音乐3以绝对路径方式添加到歌单
    musiclist->addMedia(QUrl::fromLocalFile("../../resource/2048bj/bj_music1.wav"));  // 背景音乐1以绝对路径方式添加到歌单
    musiclist->addMedia(QUrl::fromLocalFile("../../resource/2048bj/bj_music2.wav"));  // 背景音乐2以绝对路径方式添加到歌单

    musiclist->setPlaybackMode(QMediaPlaylist::Loop);  // 设置歌单循环播放

    music = new QMediaPlayer;
    music->setPlaylist(musiclist);  // 将歌单加入到背景音乐中
    music->setVolume(40);  // 初始音量为40
    music->play();  // 播放背景音乐
}

void View::renewNumber(int ** arr)
{
    clog<<"start1";
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            QImage * img=new QImage;
            if(arr[i][j]==0)
            {                                 
                 img->load("../../resource/2048bj/pic/0.jpg");
            }
            if(arr[i][j]==2)
            {
                 img->load("../../resource/2048bj/pic/2.jpg");
            }
            if(arr[i][j]==4)
            {
                 img->load("../../resource/2048bj/pic/4.jpg");
            }
            if(arr[i][j]==8)
            {
                 img->load("../../resource/2048bj/pic/8.jpg");
            }
            if(arr[i][j]==16)
            {
                 img->load("../../resource/2048bj/pic/16.jpg");
            }
            if(arr[i][j]==32)
            {
                 img->load("../../resource/2048bj/pic/32.jpg");
            }
            if(arr[i][j]==64)
            {
                 img->load("../../resource/2048bj/pic/64.jpg");
            }
            if(arr[i][j]==128)
            {
                 img->load("../../resource/2048bj/pic/128.jpg");
            }
            if(arr[i][j]==256)
            {
                 img->load("../../resource/2048bj/pic/256.jpg");
            }
            if(arr[i][j]==512)
            {
                 img->load("../../resource/2048bj/pic/512.jpg");
            }
            if(arr[i][j]==1024)
            {
                 img->load("../../resource/2048bj/pic/1024.jpg");
            }
            if(arr[i][j]==2048)
            {
                 img->load("../../resource/2048bj/pic/2048.jpg");
            }
            QImage newImg = img->scaled(200, 180);  // 更改图片大小
            p_Number[i*4+j]->setPixmap(QPixmap::fromImage(newImg));
            p_Number[i*4+j]->setAlignment(Qt::AlignCenter);
        }
    }
}

void View::renewCurrentScore(int currentScore)
{
    p_CurrentScore->setText("Score: " + QString::number(currentScore));
}

void View::displayOptimalScore(int optimalScore)
{
    p_OptimalScore->setText("BestScore: " + QString::number(optimalScore));
}

void View::renewTip(int directionID)
{
    if(directionID == 1)
    {
        p_Tip->setText("Tip: ↑");
    }
    else if(directionID == 2)
    {
        p_Tip->setText("Tip: ↓");
    }
    else if(directionID == 3)
    {
        p_Tip->setText("Tip: ←");
    }
    else if(directionID == 4)
    {
        p_Tip->setText("Tip: →");
    }
    else if(directionID == 0)
    {
        p_Tip->setText("Tip: ");
    }
}

void View::renew(int ** arr, int currentScore)
{
    renewNumber(arr);
    renewCurrentScore(currentScore);
}

void View::display(bool victory, bool gameover, int comboNumber)
{
    if(victory == 1)
    {
        p_Combo->setText("");       // 用空白内容代替文本，即隐藏
        p_Gameover->setText("");
        displayVictory();
    }
    else
    {
        if(gameover == 1)
        {
            p_Victory->setText("");
            p_Combo->setText("");
            displayGameover();
        }
        else
        {
            p_Victory->setText("");
            p_Gameover->setText("");
            p_Combo->setText("");
            if(comboNumber > 0)
            {
                displayCombo(comboNumber);
            }
        }
    }
}
